/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.1
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   用户登录对话框
*                                                                         *
 ***************************************************************************/

#include "login.h"
#include "ui_login.h"
#include <QtCore>
#include <QFile>
#include <QPalette>
#include <QList>
#include <QIntValidator>
#include <QNetworkInterface>
#include "safetynoticeform.h"
#include "hmicommondefine.h"

Login::Login(int styleId, GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{

    ui->setupUi(this);
    move(0,0);
    setStyleSheet("background-color:white;outline: none;");
    initialDisplayStyle(styleId);//修改页面显示风格

    loginStatus = new LoginStatus(globalDataIn,this);
    ipSetDialog = new IpSetDialog(this);

    haveEdited = false;//初始状态 没有被修改过

    //初始化成员变量
    controllerIpAddress.resize(4);
    controllerIpAddress[0] = 192;
    controllerIpAddress[1] = 168;
    controllerIpAddress[2] = 1;
    controllerIpAddress[3] = 169;

    hmiIpAddress.resize(10);
    hmiIpAddress[0]=192;
    hmiIpAddress[1]=168;
    hmiIpAddress[2]=1;
    hmiIpAddress[3]=109;

    QList<QHostAddress> list;
    list = QNetworkInterface::allAddresses();
    foreach (QHostAddress address, list)
    {
        //使用IPv4地址
        if(address.protocol() == QAbstractSocket::IPv4Protocol)
        {
            if (address.toString().contains("127.0."))
            {
            }
            else
            {
                //qDebug()<<address.toString();
                QStringList strlist=address.toString().split(".");
                hmiIpAddress[0] = strlist[0].toInt();
                hmiIpAddress[1] = strlist[1].toInt();
                hmiIpAddress[2] = strlist[2].toInt();
                hmiIpAddress[3] = strlist[3].toInt();
            }
        }
    }

    passwardString="123456";
    port=5050;
    selectedAccount=0;

    //
    hmiProgramVersion=tr("版本号:");

    //ui->lineEdit_port->setEnabled(false);

    ui->labelipdiscrip_3->hide();
    ui->lineEdit_port->hide();

}

Login::~Login()
{

}

void Login::show_dialog()
{
    QSettings settings(D_SHOW_STYLE_PATH, QSettings::IniFormat);
    int isShowNotice=  settings.value("Common/isShowSafetyNotice").toInt();
    if(1==isShowNotice)
    {
        SafetyNoticeForm noticeForm(this);
        noticeForm.exec();
    }

    show();
}

void Login::initSetIPFromIni(QString ctrlIp, QString ctrlPort, QString hmiIp, QString hmiPort)
{
    if(ctrlIp != "" && ctrlPort != "")
    {
        //qDebug()<<"controllerIpAddress.size() "<<controllerIpAddress.size();
        QStringList list = ctrlIp.split(".");
        if(list.size() == 4)
        {
            controllerIpAddress[0] = list[0].toInt();
            controllerIpAddress[1] = list[1].toInt();
            controllerIpAddress[2] = list[2].toInt();
            controllerIpAddress[3] = list[3].toInt();
        }

        port = ctrlPort.toInt();
    }

    if(hmiIp != "" && hmiPort != "")
    {
        //qDebug()<<"controllerIpAddress.size() "<<controllerIpAddress.size();
        QStringList list = hmiIp.split(".");
        if(list.size() == 4)
        {
            hmiIpAddress[0] = list[0].toInt();
            hmiIpAddress[1] = list[1].toInt();
            hmiIpAddress[2] = list[2].toInt();
            hmiIpAddress[3] = list[3].toInt();
        }

        //port = hmiPort.toInt();
    }
}

void Login::initLoginFromIni(int account,QString password,int lang,int editLang,QString version)
{
    selectedAccount = account;
    passwardString = password;
    selectedLanguage = lang;
    editLanguage = editLang;

    hmiProgramVersion = version;

}

void Login::setShowInitialLanguage()
{
    if(1 == selectedLanguage)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/loginEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }

    ui->retranslateUi(this);

    //子页面刷新语言
    loginStatus->changeLanguage();
    ipSetDialog->changeLanguage();

    //login 控制 delegate和mainwindow的修改
    emit controllerLanguageChangedSignal(selectedLanguage);
}

void Login::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        //ui->设置所有控件的位置和大小。
        this->setGeometry(rect().x(), rect().y(),600, 800);
        this->setFixedSize(800, 600);
        //调整ui空间的布局．
        ui->LoginWidget->setGeometry(0,100,800,500);

        ui->LoginAlertHmiIp->setGeometry(241+400,80,131,41);
        ui->GuestBtn->setGeometry(15,50,367,101);
        ui->AdminBtn->setGeometry(15,200,367,101);
        ui->labelipdiscrip_4->setGeometry(400-15-115-30-145,500,145,36);
        ui->comboBox->setGeometry(400-15-115,490,115,50);

        ui->labelpwd->setGeometry(410,90,126,35);
        ui->Pwd->setGeometry(530,90,230,35);
        ui->labelipdiscrip->setGeometry(410,170,126,35);
        ui->widgetIp->setGeometry(530,170,239,38);
        ui->labelipdiscrip_2->setGeometry(410,250,126,35);
        ui->widgetIp_2->setGeometry(527,250,239,35);


        ui->LoginBtn->setGeometry(200-57+400,390,115,50);




        break;
    default:
        break;
    }
}

void Login::on_LoginBtn_clicked()
{
    // 获取界面数据
    getViewInput();
    emit controllerLoginSignal(controllerIpAddress,hmiIpAddress,QString::number(port),passwardString,selectedAccount);
}

void Login::on_GuestBtn_clicked()
{
    selectedAccount=0;
    updateShow();
}

void Login::on_AdminBtn_clicked()
{
    selectedAccount=1;
    updateShow();
}

void Login::updateShow()
{
    //设置语言显示
    setShowInitialLanguage();

    //控件 显示
    ui->Pwd->setEchoMode(QLineEdit::Password);
    //把成员变量显示到控件
    QString b;
    b=QString::number(controllerIpAddress[0]);
    ui->lineEdit_ip1->setText(b);

    b=QString::number(controllerIpAddress[1]);
    ui->lineEdit_ip2->setText(b);

    b=QString::number(controllerIpAddress[2]);
    ui->lineEdit_ip3->setText(b);

    b=QString::number(controllerIpAddress[3]);
    ui->lineEdit_ip4->setText(b);

    b=QString::number(hmiIpAddress[0]);
    ui->lineEdit_hmi_ip1->setText(b);

    b=QString::number(hmiIpAddress[1]);
    ui->lineEdit_hmi_ip2->setText(b);

    b=QString::number(hmiIpAddress[2]);
    ui->lineEdit_hmi_ip3->setText(b);

    b=QString::number(hmiIpAddress[3]);
    ui->lineEdit_hmi_ip4->setText(b);

    ui->Pwd->setText(passwardString);

    b=QString::number(port);
    ui->lineEdit_port->setText(b);

    ui->comboBox->setCurrentIndex(selectedLanguage);

    //ui->CurrTimeLabel->setText(timeLabel);

    ui->label_loginstatus->setText(tr("版本号") + ":" + hmiProgramVersion);

    switch(selectedAccount)
    {
        case 0:
        {
            ui->GuestBtn->setDisabled(true);
            ui->AdminBtn->setDisabled(false);
        }
        break;
        case 1:
        {
            ui->GuestBtn->setDisabled(false);
            ui->AdminBtn->setDisabled(true);
        }
        break;
        default:
        {

        }
        break;
    }

    //全选高亮标志
    isEditing = false;
}

void Login::getViewInput()
{
    //获取所有控件到成员变量
    controllerIpAddress[0]=ui->lineEdit_ip1->text().toInt();
    controllerIpAddress[1]=ui->lineEdit_ip2->text().toInt();
    controllerIpAddress[2]=ui->lineEdit_ip3->text().toInt();
    controllerIpAddress[3]=ui->lineEdit_ip4->text().toInt();

    hmiIpAddress[0]=ui->lineEdit_hmi_ip1->text().toInt();
    hmiIpAddress[1]=ui->lineEdit_hmi_ip2->text().toInt();
    hmiIpAddress[2]=ui->lineEdit_hmi_ip3->text().toInt();
    hmiIpAddress[3]=ui->lineEdit_hmi_ip4->text().toInt();

    passwardString=ui->Pwd->text();

    port=ui->lineEdit_port->text().toInt();

    selectedLanguage=ui->comboBox->currentIndex();
}

void Login::on_LoginAlertHmiIp_clicked()
{
    emit hmiIpSettingSignal();
}


void Login::on_lineEdit_ip4_editingFinished()
{
    //qDebug()<<"editingFinished";
    getViewInput();
    isEditing = false;
}

void Login::on_lineEdit_ip3_editingFinished()
{
   getViewInput();
   isEditing = false;
}

void Login::on_lineEdit_ip2_editingFinished()
{
    getViewInput();
    isEditing = false;
}

void Login::on_lineEdit_ip1_editingFinished()
{
    getViewInput();
    isEditing = false;
}

void Login::on_lineEdit_ip1_cursorPositionChanged(int arg1, int arg2)
{
    if(!isEditing)
    {
        ui->lineEdit_ip1->selectAll();
        isEditing = true;
    }
}

void Login::on_lineEdit_ip2_cursorPositionChanged(int arg1, int arg2)
{
    if(!isEditing)
    {
        ui->lineEdit_ip2->selectAll();
        isEditing = true;
    }
}

void Login::on_lineEdit_ip3_cursorPositionChanged(int arg1, int arg2)
{
    if(!isEditing)
    {
        ui->lineEdit_ip3->selectAll();
        isEditing = true;
    }
}

void Login::on_lineEdit_ip4_cursorPositionChanged(int arg1, int arg2)
{   
    if(!isEditing)
    {
        //qDebug()<<"cursorPositionChanged";
        ui->lineEdit_ip4->selectAll();
        isEditing = true;
    }
}

void Login::on_lineEdit_ip1_textEdited(const QString &arg1)
{
    isEditing = true;

    haveEdited = true;//被修改过,delegate才会判断
}

void Login::on_lineEdit_ip2_textEdited(const QString &arg1)
{
    isEditing = true;

    haveEdited = true;
}

void Login::on_lineEdit_ip3_textEdited(const QString &arg1)
{
    isEditing = true;

    haveEdited = true;
}

void Login::on_lineEdit_ip4_textEdited(const QString &arg1)
{
    //qDebug()<<"textEdited";
    isEditing = true;

    haveEdited = true;
}

void Login::on_comboBox_activated(int index)
{
    //下拉选择后,跟新了
    selectedLanguage = index;

    //唯一需要调用updateShow
    updateShow();
}
